package com.juneng.utils;

import cn.hutool.core.util.StrUtil;
import com.juneng.entity.OrderMsg;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 比较工具类
 */
public class CompareUtil {

    public static Integer numMemory(List<String> list1, List<String> list2) {
        int num = 0;
        for (int i = 0; i < list1.size(); i++) {
            if (StringUtils.isEmpty(list2.get(i)) || StringUtils.isEmpty(list1.get(i))) {
                break;
            }
            Integer integer = Integer.valueOf(list1.get(i));
            Integer integer1 = Integer.valueOf(list2.get(i));
            if (integer == integer1) {
                num = num + 1;
            }
        }
        return num;
    }

    public static Integer springMemory(List<String> list1, List<String> list2) {
        int num = 0;
        for (int i = 0; i < list1.size(); i++) {
            if (StringUtils.isEmpty(list2.get(i)) || StringUtils.isEmpty(list1.get(i))) {
                break;
            }
            if (list1.get(i).equalsIgnoreCase(list2.get(i))) {
                num = num + 1;
            }
        }
        return num;
    }


    /**
     * 将List<String>转化为String字符串显示，元素之间用“，”号隔开
     *
     * @param list 要处理的list
     * @return 返回 元素之间用“，”号隔开的字符串
     */
    public static String ArrayListToString1(List<Integer> list) {

        if (list == null || list.size() == 0) {
            return null;
        }

        boolean isFirst = true;
        StringBuffer result = new StringBuffer();
        for (Integer s : list) {
            if (isFirst) {
                isFirst = false;
            } else {
                result.append(",");
            }
            result.append(s);
        }

        return result.toString();
    }

    /**
     * 将List<String>转化为String字符串显示，元素之间用“，”号隔开
     *
     * @param list 要处理的list
     * @return 返回 元素之间用“，”号隔开的字符串
     */
    public static String ArrayListToString2(List<String> list) {

        if (list == null || list.size() == 0) {
            return null;
        }
        boolean isFirst = true;
        StringBuffer result = new StringBuffer();
        for (String s : list) {
            if (isFirst) {
                isFirst = false;
            } else {
                result.append(",");
            }
            result.append(s);
        }

        return result.toString();
    }


    /**
     * 将String 转List<Integer>
     *
     * @param str
     * @return
     */
    public static List<Integer> stringListToInteger(String str) {
        List<Integer> integers = new ArrayList<>();
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        String[] split = str.split(",");
        for (int i = 0; i < split.length; i++) {
            integers.add(Integer.valueOf(split[i]));
        }
        return integers;
    }

    /**
     * 将String 转List<Long>
     *
     * @param str
     * @return
     */
    public static List<Long> stringListToLong(String str) {
        List<Long> longList = new ArrayList<>();
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        String[] split = str.split(",");
        for (int i = 0; i < split.length; i++) {
            longList.add(Long.parseLong(split[i]));
        }
        return longList;
    }

    /**
     * 将String 转List<String>
     *
     * @param str
     * @return
     */
    public static List<String> stringListToString(String str) {
        List<String> strings = new ArrayList<>();
        if (!StrUtil.isNotBlank(str)) {
            return null;
        }
        String[] split = str.split(",");
        for (int i = 0; i < split.length; i++) {
            strings.add(split[i]);
        }
        return strings;
    }

    /**
     * 将String 转List<String>
     *
     * @param str
     * @return
     */
    public static List<String> stringListToString1(String str) {
        List<String> strings = new ArrayList<>();
        if (!StrUtil.isNotBlank(str)) {
            return null;
        }
        String[] split = str.split("/");
        for (int i = 0; i < split.length; i++) {
            strings.add(split[i]);
        }
        return strings;
    }


    /**
     * 将String 转List<Integer>
     *
     * @param str
     * @return
     */
    public static List<Integer> stringListToInteger(List<String> str) {
        if (str == null || str.size() == 0) {
            return null;
        }
        List<Integer> integers = new ArrayList<>();
        for (String s : str) {
            integers.add(Integer.valueOf(s));
        }
        return integers;
    }

    /**
     * 将List<String>转化为String字符串显示，元素之间用“，”号隔开
     *
     * @param list 要处理的list
     * @return 返回 元素之间用“，”号隔开的字符串
     */
    public static String LongToString(List<Long> list) {

        if (list == null || list.size() == 0) {
            return null;
        }

        boolean isFirst = true;
        StringBuffer result = new StringBuffer();
        for (Long s : list) {
            if (isFirst) {
                isFirst = false;
            } else {
                result.append(",");
            }
            result.append(s);
        }

        return result.toString();
    }


    public static String ListToString(List<String> list) {

        if (list == null || list.size() == 0) {
            return null;
        }

        boolean isFirst = true;
        StringBuffer result = new StringBuffer();
        for (String s : list) {
            if (isFirst) {
                isFirst = false;
            } else {
                result.append(",");
            }
            result.append(s);
        }

        return result.toString();
    }


    /**
     * 占比计算保留小数的位数方法
     * 转成百分数
     * 当前数除以总数
     *
     * @param num1 ,num2  num1/num2
     * @return rate  保留2位小数的
     */
    public static String division(int num1, int num2) {
        String rate = "0.00";
        //定义格式化起始位数
        String format = "0.00";
        if (num2 != 0 && num1 != 0) {
            DecimalFormat dec = new DecimalFormat(format);
            rate = dec.format((double) num1 / num2 * 100);
            while (true) {
                if (rate.equals(format)) {
                    format = format + "0";
                    DecimalFormat dec1 = new DecimalFormat(format);
                    rate = dec1.format((double) num1 / num2 * 100);
                } else {
                    break;
                }
            }
        } else if (num1 != 0 && num2 == 0) {
            rate = "100";
        }
        return rate;
    }


    /**
     * 随机获取英文+数字（用户名）
     *
     * @param engCode 小写英文的数量
     * @return
     */
    public static String verifyUserName(int engCode) {
        //声明一个StringBuffer存储随机数
        /* StringBuffer sb = new StringBuffer();*/
        StringBuffer stringBuffer = new StringBuffer();
        String[] englishCodeArray = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "S", "Y", "Z"};
        /*    char[] numCodeArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};*/
        Random random = new Random();
        boolean isFirst = true;
        //获取英文
        for (int i = 0; i < engCode; i++) {
            String num = englishCodeArray[random.nextInt(englishCodeArray.length)];
            stringBuffer.append(num);
            if (i < (engCode - 1)) {
                stringBuffer.append(",");
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 随机获取英文+数字（用户名）
     *
     * @param num 小写英文的数量
     * @return
     */
    public static List<Integer> verifyNUm(int num) {
        Integer[] englishCodeArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
        Random random = new Random();
        List<Integer> arr = new ArrayList<>();
        //获取数字
        for (int i = 0; i < num; i++) {
            Integer num1 = englishCodeArray[random.nextInt(englishCodeArray.length)];
            arr.add(num1);
        }
        return arr;
    }

    /**
     * 随机获取英文+数字（用户名）
     *
     * @param
     * @return
     */
    public static Integer correctNum() {
        Integer[] englishCodeArray = {1, 2, 3, 4, 5, 6, 0};
        Random random = new Random();
        //获取数字
        Integer num1 = englishCodeArray[random.nextInt(englishCodeArray.length)];
        return num1;
    }

    public static Integer subjectNum() {
        Integer[] englishCodeArray = {18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28};
        Random random = new Random();
        //获取数字
        Integer num1 = englishCodeArray[random.nextInt(englishCodeArray.length)];
        return num1;
    }


    // 获取运算符
    private static String getOp(int index) {
        String string = "";
        switch (index) {
            case 0:
                string = "+";
                break;
            case 1:
                string = "-";
                break;
            case 2:
                string = "*";
                break;
            case 3:
                string = "➗";
                break;
        }
        return string;
    }

    // 计算答案
    private static int jisuan(int a, int b, int index) {
        int sum = 0;
        switch (index) {
            case 0:
                sum = a + b;
                break;
            case 1:
                sum = a - b;
                break;
            case 2:
                sum = a * b;
                break;
            case 3:
                sum = a / b;
                break;
        }
        return sum;
    }

    /**
     * 自定义进制(0,1没有加入,容易与o,l混淆)，数组顺序可进行调整增加反推难度，A用来补位因此此数组不包含A，共31个字符。
     */
    private static final char[] BASE = new char[]{'H', 'V', 'E', '8', 'S', '2', 'D', 'Z', 'X', '9', 'C', '7', 'P',
            '5', 'I', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'W', 'Y', 'L', 'T', 'N', '6', 'B', 'G', 'Q'};

    /**
     * A补位字符，不能与自定义重复
     */
    private static final char SUFFIX_CHAR = 'A';

    /**
     * 进制长度
     */
    private static final int BIN_LEN = BASE.length;

    /**
     * 生成邀请码最小长度
     */
    private static final int CODE_LEN = 6;

    /**
     * ID转换为邀请码
     *
     * @param id
     * @return
     */
    public static String idToCode(Long id) {
        char[] buf = new char[BIN_LEN];
        int charPos = BIN_LEN;

        // 当id除以数组长度结果大于0，则进行取模操作，并以取模的值作为数组的坐标获得对应的字符
        while (id / BIN_LEN > 0) {
            int index = (int) (id % BIN_LEN);
            buf[--charPos] = BASE[index];
            id /= BIN_LEN;
        }

        buf[--charPos] = BASE[(int) (id % BIN_LEN)];
        // 将字符数组转化为字符串
        String result = new String(buf, charPos, BIN_LEN - charPos);

        // 长度不足指定长度则随机补全
        int len = result.length();
        if (len < CODE_LEN) {
            StringBuilder sb = new StringBuilder();
            sb.append(SUFFIX_CHAR);
            Random random = new Random();
            // 去除SUFFIX_CHAR本身占位之后需要补齐的位数
            for (int i = 0; i < CODE_LEN - len - 1; i++) {
                sb.append(BASE[random.nextInt(BIN_LEN)]);
            }

            result += sb.toString();
        }

        return result;
    }

    /**
     * 邀请码解析出ID<br/>
     * 基本操作思路恰好与idToCode反向操作。
     * 按钮图标
     *
     * @param code
     * @return
     */
    public static Long codeToId(String code) {
        char[] charArray = code.toCharArray();
        long result = 0L;
        for (int i = 0; i < charArray.length; i++) {
            int index = 0;
            for (int j = 0; j < BIN_LEN; j++) {
                if (charArray[i] == BASE[j]) {
                    index = j;
                    break;
                }
            }

            if (charArray[i] == SUFFIX_CHAR) {
                break;
            }

            if (i > 0) {
                result = result * BIN_LEN + index;
            } else {
                result = index;
            }
        }

        return result;

    }


    public static void main(String[] args) {
        String string = null;
        if (StrUtil.isNotBlank(string)) {
            System.out.println(123);
        }
    }

}
